// Copyright 2022 jeff.li. and/or its affiliates.

#include <tbir/runtime/global_type_index.h>
#include <tbir/runtime/object.h>
#include <tbir/runtime/runtime_port.h>

namespace tbir::runtime {

    TURBO_EXPORT const char *TypeIndex2Str(int32_t type_code) {
        switch (type_code) {
            case TypeIndex::kRuntimeInteger:
                return "int";
            case TypeIndex::kRuntimeFloat:
                return "float";
            case TypeIndex::kRuntimeOpaqueHandle:
                return "handle";
            case TypeIndex::kRuntimeNullptr:
                return "nullptr";
            case TypeIndex::kTbirByteArray:
                return "ByteArray";
            case TypeIndex::kRuntimeDataType:
                return "DLDataType";
            case TypeIndex::kRuntimeContext:
                return "TbirContext";
            case TypeIndex::kRuntimeDLTensorHandle:
                return "ArrayHandle";
            case TypeIndex::kRuntimePackedFuncHandle:
                return "TbirFunctionHandle";
            case TypeIndex::kRuntimeObjectRValueRefArg:
                return "ObjectRValueRefArg";
            case TypeIndex::kRoot:
                return "Object";
            case TypeIndex::kRuntimeString:
                return "String";
            case TypeIndex::kRuntimeUnicode:
                return "Unicode";
            case TypeIndex::kRuntimeList:
                return "List";
            case TypeIndex::kRuntimeDict:
                return "Dict";
            case TypeIndex::kRuntimeSet:
                return "Set";
            case TypeIndex::kRuntimeFTList:
                return "FTList";
            case TypeIndex::kRuntimeFTDict:
                return "FTDict";
            case TypeIndex::kRuntimeFTSet:
                return "FTSet";
            case TypeIndex::kRuntimeNDArray:
                return "NDArray";
            case TypeIndex::kRuntimeFile:
                return "File";
            case TypeIndex::kRuntimeTrie:
                return "Trie";
            case TypeIndex::kRuntimeTuple:
                return "Tuple";
            case TypeIndex::kRuntimeRegex:
                return "Regex";
            case TypeIndex::kRuntimeIterator:
                return "Iterator";
            case TypeIndex::kRuntimeUserData:
                return "UserData";
            case TypeIndex::kRuntimeOpaqueObject:
                return "OpaqueObject";
            case TypeIndex::kRuntimeUnknown:
                return "Unknown";
            default:
                if (type_code >= 0) {
                    string_view name;
                    if (Object::TryTypeIndex2Key(type_code, &name)) {
                        return name.data();
                    } else {
                        return "Object";
                    }
                } else {
                    return "Unknown";
                }
        }
    }

    TURBO_EXPORT int32_t Str2TypeIndex(string_view str) {
        if (str == "int") {
            return TypeIndex::kRuntimeInteger;
        } else if (str == "float") {
            return TypeIndex::kRuntimeFloat;
        } else if (str == "handle") {
            return TypeIndex::kRuntimeOpaqueHandle;
        } else if (str == "nullptr") {
            return TypeIndex::kRuntimeNullptr;
        } else if (str == "Object") {
            return TypeIndex::kRoot;
        } else if (str == "String") {
            return TypeIndex::kRuntimeString;
        } else if (str == "Unicode") {
            return TypeIndex::kRuntimeUnicode;
        } else if (str == "List") {
            return TypeIndex::kRuntimeList;
        } else if (str == "Dict") {
            return TypeIndex::kRuntimeDict;
        } else if (str == "Set") {
            return TypeIndex::kRuntimeSet;
        } else if (str == "NDArray") {
            return TypeIndex::kRuntimeNDArray;
        } else if (str == "File") {
            return TypeIndex::kRuntimeFile;
        } else if (str == "Trie") {
            return TypeIndex::kRuntimeTrie;
        } else if (str == "Tuple") {
            return TypeIndex::kRuntimeTuple;
        } else if (str == "Regex") {
            return TypeIndex::kRuntimeRegex;
        } else if (str == "Iterator") {
            return TypeIndex::kRuntimeIterator;
        } else if (str == "UserData") {
            return TypeIndex::kRuntimeUserData;
        } else if (str == "OpaqueObject") {
            return TypeIndex::kRuntimeOpaqueObject;
        } else {
            return Object::TypeKey2Index(str);
        }
    }

}  // namespace tbir::runtime
